package com.tjsinfo.tjsoa.common;

import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.text.DecimalFormat;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Calendar;
import java.util.Collections;
import java.util.Date;
import java.util.Enumeration;
import java.util.HashMap;
import java.util.HashSet;
import java.util.Iterator;
import java.util.List;
import java.util.Map;
import java.util.Properties;
import java.util.Set;
import java.util.UUID;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

import javax.servlet.http.HttpServletRequest;

import com.tjsinfo.tjsoa.system.vo.User;

public class ToolKit {
	public static Map<String,String> getParameterNames(HttpServletRequest request){
		Enumeration<String> enums=request.getParameterNames();
		Map<String,String> parameterNames=new HashMap<String, String>();
		while (enums.hasMoreElements()) {
			String elem = (String) enums.nextElement();
			parameterNames.put(elem, request.getParameter(elem));
		}
		return parameterNames;
	}
	public static String getUUID(){
		UUID uuid = UUID.randomUUID();
		return uuid.toString().replace("-", "");
	}
	public static User getSessionUser(HttpServletRequest request){
		return (User)request.getSession().getAttribute("user");
	}
	public static Properties getProperties(String url,Object object){
		Properties props = new Properties();
	    ClassLoader cl = object.getClass().getClassLoader();
		InputStream is = cl.getResourceAsStream(url); 
		try {
			props.load(is);
		} catch (IOException e1) {
			// TODO Auto-generated catch block
			e1.printStackTrace();
		}
		return props;
		
	}
	/**
	 * 将有逗号间隔的字符串分隔为数组
	 * @param 逗号间隔的字符串（例如： a,b,c.d）
	 * @return 字符串数组
	 */
	public static String[] splitString(String str)
	{
		String[] strarray = new  String[1];
		if (str != null &&!"".equals(str.trim())) {
			if (str.indexOf(",") != -1) {
				strarray = str.split(",");
			} else {
				
				strarray[0] = str;
			}
		} else {
			;
		}
		return strarray;
	}
	/**
	 * 日期格式转换
	 * @param 日期对象
	 * @param 格式
	 * @return
	 */
	public static String getFormatDate(java.util.Date thisDate, String format) {
		if (thisDate == null)
			return "";
		SimpleDateFormat simpleDateFormat = new SimpleDateFormat(format);
		return simpleDateFormat.format(thisDate);
	}
	
	/**
	 * 返回周日
	 * @param date
	 * @return
	 */
    public   static   String   getSunday(Date   date){   
        Calendar   c   =   Calendar.getInstance();   
        c.setTime(date);   
        c.set(Calendar.DAY_OF_WEEK,Calendar.SUNDAY);   
        return   new   SimpleDateFormat("yyyy-MM-dd").format(c.getTime());   
      }    
     /**
      * 返回周一
      * @param date
      * @return
      */
      public   static   String   getMonday(Date   date){   
        Calendar   c   =   Calendar.getInstance();   
        c.setTime(date);   
        c.set(Calendar.DAY_OF_WEEK,Calendar.MONDAY);   
        return   new   SimpleDateFormat("yyyy-MM-dd").format(c.getTime());   
      }   
      /**
       * 返回周二
       * @param date
       * @return
       */
      public   static   String   getTuesday(Date   date){   
          Calendar   c   =   Calendar.getInstance();   
          c.setTime(date);   
          c.set(Calendar.DAY_OF_WEEK,Calendar.TUESDAY);   
          return   new   SimpleDateFormat("yyyy-MM-dd").format(c.getTime());   
        } 
      /**
       * 返回周3
       * @param date
       * @return
       */
      public   static   String   getWednesday(Date   date){   
          Calendar   c   =   Calendar.getInstance();   
          c.setTime(date);   
          c.set(Calendar.DAY_OF_WEEK,Calendar.WEDNESDAY);   
          return   new   SimpleDateFormat("yyyy-MM-dd").format(c.getTime());   
        } 
      /**
       * 返回周4
       * @param date
       * @return
       */
      public   static   String   getThursday(Date   date){   
          Calendar   c   =   Calendar.getInstance();   
          c.setTime(date);   
          c.set(Calendar.DAY_OF_WEEK,Calendar.THURSDAY);   
          return   new   SimpleDateFormat("yyyy-MM-dd").format(c.getTime());   
        } 
      /**
       * 返回周5
       * @param date
       * @return
       */
      public   static   String   getFriday(Date   date){   
        Calendar   c   =   Calendar.getInstance();   
        c.setTime(date);   
        c.set(Calendar.DAY_OF_WEEK,Calendar.FRIDAY);         
        return   new   SimpleDateFormat("yyyy-MM-dd").format(c.getTime());       
      }   
      /**
       * 返回周6
       * @param date
       * @return
       */
      public   static   String   getSaturday(Date   date){   
          Calendar   c   =   Calendar.getInstance();   
          c.setTime(date);   
          c.set(Calendar.DAY_OF_WEEK,Calendar.SATURDAY);   
          return   new   SimpleDateFormat("yyyy-MM-dd").format(c.getTime());   
        } 
     public static String getRodomer()
  	{
  		SimpleDateFormat format = new SimpleDateFormat("yyyyMMddkkmmssSS");
  		return format.format(new Date());
  	}
 	/*
 	 * 格式化日期
 	 */
 	public  static String FormatDate(Date date ){
 		SimpleDateFormat a=new SimpleDateFormat("yyyy-MM-dd");
 		String dateF=a.format(date);
 		return dateF;
 	}
 	
 	/*
 	 * 格式化日期

 	 */
 	public  static String FormatDate(Date date ,String formate){
 		SimpleDateFormat a=new SimpleDateFormat(formate);
 		String dateF=a.format(date);
 		return dateF;
 	}
 	
 	
	/*
 	 * 格式化日期
 	 */
 	public  static Date FormatDate(String date ){
 		SimpleDateFormat a=new SimpleDateFormat("yyyy-MM-dd");
 		Date date2 = new Date();
 		try {
 			date2 = a.parse(date);
		} catch (ParseException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		return date2;
 	}
 	  // 日期转化为大小写  
 	 public static String dataToUpper(Date date) {  
 	         Calendar ca = Calendar.getInstance();     
 	         ca.setTime(date);     
 	         int year = ca.get(Calendar.YEAR);     
 	         int month = ca.get(Calendar.MONTH) + 1;     
 	         int day = ca.get(Calendar.DAY_OF_MONTH);  
 	         return numToUpper(year) + "年" + monthToUppder(month) + "月" + dayToUppder(day) + "日";  
 	 }  
 	    
 	  // 将数字转化为大写   
 	 public static String numToUpper(int num) {  
 	        //String u[] = {"零","壹","贰","叁","肆","伍","陆","柒","捌","玖"};  
 	        String u[] = {"○","一","二","三","四","五","六","七","八","九"};  
 	        char[] str = String.valueOf(num).toCharArray();  
 	         String rstr = "";  
 	         for (int i = 0; i < str.length; i++) {  
 	                rstr = rstr + u[Integer.parseInt(str[i] + "")];  
 	          }  
 	         return rstr;  
 	  }  
 	   
 	  // 月转化为大写  
 	  public static String monthToUppder(int month) {  
 	           if(month < 10) {  
 	                   return numToUpper(month);          
 	                   } else if(month == 10){  
 	                  return "十";  
 	           } else {  
 	                  return "十" + numToUpper(month - 10);  
 	           }  
 	  }  
 	    
 	  // 日转化为大写  
 	 public static String dayToUppder(int day) {  
 	          if(day < 20) {  
 	                   return monthToUppder(day);  
 	          } else {  
 	                   char[] str = String.valueOf(day).toCharArray();  
 	                   if(str[1] == '0') {  
 	                            return numToUpper(Integer.parseInt(str[0] + "")) + "十";  
 	                   }else {  
 	                             return numToUpper(Integer.parseInt(str[0] + "")) + "十" + numToUpper(Integer.parseInt(str[1] + ""));  
 	                    }  
 	         }  
 	 }  
 	 /**
 	  * 取数字的每一位
 	  * @param 位数
 	  * @param 数字
 	  * @param 是否是小数点以后
 	  * @return
 	  */
 	 public static String weiToMath(Integer weishu,Double math,Boolean isbehend){
  		java.text.DecimalFormat df =new java.text.DecimalFormat("#.00");
 		 String mathstr = df.format(math);
 		 String[] mathstrarray = mathstr.split("");
 		 List<String> list =  Arrays.asList(mathstrarray);
 		 Collections.reverse(list);//反转列表排序
 		 mathstrarray = (String[]) list.toArray();
 			 if(mathstrarray.length - 1> weishu){
 	 			return   mathstrarray[weishu];
 	 		 } 
 	 		 else 
 	 		 {
 	 			 if (isbehend) {
 					return "0";
 				} else {
 					return "";	
 				}
 	 		 }
 		 }
 	/**
 	   * 求某年下的第几个星期的日期 返回java.uilt.Date 类型日期 时间time为当前机器时间
 	   * @param year 要获得的年
 	   * @param week 第几个星期
 	   * @param flag 是否是第一天还是最后一天,当为true时返回第一天,false则返回最后一天
 	   * @return java.uilt.Date 类型日期
 	   * @例如 getDayByWeek(2002,2,true) 返回Tue Jan 08 14:11:57 CST 2002 
 	   */
 	  public static Date getDayByWeek(int year,int week,boolean flag)
 	  {
 	   Calendar cal=Calendar.getInstance();
 	   cal.set(Calendar.YEAR,year);
 	   cal.set(Calendar.WEEK_OF_YEAR,week);
 	   if(!flag)
 	    cal.setTimeInMillis(cal.getTimeInMillis()+6*24*60*60*1000);
 	    return cal.getTime();  
 	  } 
 	  
 	 
 	  /**
 	   * 过滤List<String>集合中重复的字符串
 	   * @param List<String>集合
 	   * @return List<String>集合
 	   */
 	 public static List<String> removeDuplicateWithOrder(List<String> arlList)
 	  {
 	  Set<String> set = new HashSet<String>();
 	  List newList = new ArrayList();
 	  for (Iterator<String> iter = arlList.iterator();    iter.hasNext(); ) {
 	  String element = iter.next();
 	    if (set.add(element))
 	       newList.add(element);
 	     }
 	     arlList.clear();
 	     arlList.addAll(newList);
 	     return arlList;
 	 }
 	 /**
 	  * 将List转化为String数组
 	  * @param List<String> 字符串List
 	  * @return
 	  */
 	 public static String[] listToArray(List<String> list){
 		return  list.toArray(new String[]{}); 
 	 }
 	 /**
 	  * 将字符串数组转化为拼接字符串(用","分割 )
 	  * @param 字符串数组
 	  * @return 字符串
 	  */
 	 public static String arrayToString(String[] stringarray){
 		String message = "";
 		 for (String temp : stringarray) {
 			 message+=",";
 			 message+=temp;
		}
 		message+=",";
 		return message;
 	 }
 	 /**
 	  * 单元测试方法
 	  * @param args
 	  */
 	 public static void main(String[] args) {
 		 Date date = FormatDate("2013-12-12");
 		 System.out.println(FormatDate(date, "yyyy-M-dd"));
	}
 	 
 	 
 	public static boolean copyFile(String src, String des){ 
 		File srcFile, desFile; 
 		srcFile = new File(src); 
 		desFile = new File(des); 
 		FileInputStream fis = null; 
 		FileOutputStream fos = null; 

 		try{ 
 		desFile.createNewFile(); 
 		fis = new FileInputStream(srcFile); 
 		fos = new FileOutputStream(desFile); 
 		int bytesRead; 
 		byte[] buf = new byte[4 * 1024]; //4K buffer 
 		while((bytesRead = fis.read(buf)) != -1){ 
 		fos.write(buf, 0, bytesRead); 
 		} 
 		fos.flush(); 
 		fos.close(); 
 		fis.close(); 
 		}catch(IOException e) 
 		{ 
 		System.out.println(e); 
 		return false; 
 		} 
 		return true; 
 		} 
 	
 	public static String getChineseWeek(Integer week){
 		if(week == 0){
 			return "日";
 		}
 		if(week == 1){
 			return "一";
 		}
 		if(week == 2){
 			return "二";
 		}
 		if(week == 3){
 			return "三";
 		}
 		if(week == 4){
 			return "四";
 		}
 		if(week == 5){
 			return "五";
 		}
 		if(week == 6){
 			return "六";
 		}
 		return "";
 	}
 	
 	  public static String delHTMLTag(String htmlStr){ 
          String regEx_script="<script[^>]*?>[\\s\\S]*?<\\/script>"; //定义script的正则表达式 
          String regEx_style="<style[^>]*?>[\\s\\S]*?<\\/style>"; //定义style的正则表达式 
          String regEx_html="<[^>]+>"; //定义HTML标签的正则表达式 
          
          Pattern p_script=Pattern.compile(regEx_script,Pattern.CASE_INSENSITIVE); 
          Matcher m_script=p_script.matcher(htmlStr); 
          htmlStr=m_script.replaceAll(""); //过滤script标签 
          
          Pattern p_style=Pattern.compile(regEx_style,Pattern.CASE_INSENSITIVE); 
          Matcher m_style=p_style.matcher(htmlStr); 
          htmlStr=m_style.replaceAll(""); //过滤style标签 
          
          Pattern p_html=Pattern.compile(regEx_html,Pattern.CASE_INSENSITIVE); 
          Matcher m_html=p_html.matcher(htmlStr); 
          htmlStr=m_html.replaceAll(""); //过滤html标签 

         return htmlStr.trim(); //返回文本字符串 
      } 
 	  
 	  /**
 	   * 保留数字小数点后两位
 	   * @param number
 	   * @return
 	   */
 	  public static String doubleToTwo(Double number){
 		 DecimalFormat    df   = new DecimalFormat("######0.00");   
 		 return 	df.format(number); 
 	  }
}
